package com.xujiang.myblog;

import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;

import com.miguelbcr.ui.rx_paparazzo.RxPaparazzo;
import com.umeng.message.PushAgent;
import com.xujiang.myblog.model.PreferUser;
import com.xujiang.myblog.network.okhttp.OkHttp3Utils;
import com.xujiang.myblog.network.retrofit.RetrofitUtils;
import com.xujiang.myblog.tools.Common;
import com.xujiang.myblog.url.Urls;
import com.xujiang.myblog.util.PreferencesUtils;
import com.xujiang.umengutils.ourUmengUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by xujiang on 2017/4/14.
 */

public class MApplication  extends MultiDexApplication{
    private final String TAG = MApplication.class.getSimpleName();
    public static PushAgent getmPushAgent() {
        return mPushAgent;
    }

    private static MApplication singleton; //返回应用程序实例
    public static MApplication getInstance()
    { return singleton; }
    //来自友盟的变量，使用友盟需要这个
    private static PushAgent mPushAgent;
    @Override
    public void onCreate() {
        super.onCreate();
        //友盟初始化
        umengInit();
        singleton = this;

        Common.init(this, "MyBlog", BuildConfig.DEBUG);
        //okhttp3的初始化
        okHttpoInit();
        //retrofit的初始化
        RetrofitUtils.get().init(Urls.BASE_URL,OkHttp3Utils.get().getClient());
        //Preferences存储的初始化
        PreferencesUtils.init(this);

        PreferUser.get().readLoginInfo();  //读取prefer里的数据，并在全局保持一个变量不被销毁
        //github上的图片选择工具，支持rxjava
        RxPaparazzo.register(this);


    }

    private void okHttpoInit() {
        //okhttp3的初始化，后面retrofit需要使用okhttp3对其进行初始化
        OkHttp3Utils.Init init = OkHttp3Utils.init().setErrorHandleListener(new OkHttp3Utils.ErrorHandleListener() {

            @Override
            public Response checkError(Response response) throws Exception {
                //每个使用OkHttpUtils的请求都会检查
                if (!response.isSuccessful()) {
                    throw new Exception("ERR_NETWORK:" + response.code());
                }
                String error = response.header("error");
                if (!TextUtils.isEmpty(error)) {
                    throw new Exception(error);
                }
                return response;
            }
        }).addHeaderPlus(new OkHttp3Utils.HeaderPlusListener() {

            @Override
            public Request.Builder headerInterceptor(Request originalRequest, Request.Builder builder, Map<String, Object> parameters) {
                if (true) {
                    //只要登录就会添加header信息
//                    builder.addHeader("uniqueId", "fjdskg");
//                    builder.addHeader("tk", "gfsdf");
                }
                return builder;
            }
        }).setBuilder(new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS).readTimeout(60, TimeUnit.SECONDS));
        init.setDefaultCache().initialization();  //okhttp的初始化
    }

    private void umengInit() {

        mPushAgent= PushAgent.getInstance(this);
        mPushAgent.setMessageChannel("mytest"); //设置我的推广渠道
        //注册友盟，并设置日志输出为true
        ourUmengUtils.setPushAgent(mPushAgent);
        try {
            ourUmengUtils.umengRegister();
        } catch (Exception e) {
            e.printStackTrace();
        }
        mPushAgent.setDebugMode(true); //日志输出设置
        // mPushAgent.getRegistrationId();  //手动获取device token
    }
}
